﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

using RRRSRoguelike.Enums;
using RRRSRoguelike.Helpers;
using RRRSRoguelike.Managers;
using RRRSRoguelikeData;

namespace RRRSRoguelike.Entities
{
    /// <summary>
    /// base class for all living entities
    /// </summary>
    public abstract class Creature : Tile
    {

        #region variables
        /// <summary>
        /// holds all the creature data from the data library 
        /// </summary>
        private CreatureData creatureData = new CreatureData();
        /// <summary>
        /// Event called when creature dies.
        /// </summary>
        /// <param name="sender"></param>
        public event CreatureDeadHandler CreatureDeadEvent;

        public Dictionary<Direction,int> Moves = new Dictionary<Direction,int>();

        public bool hasMoved = false;
        public bool hasJumped = false;
        public bool hasFired = false;
        
        private ActionManager AM = DungeonManager.Instance.ActionManager;

        #endregion

        #region constructor
        public Creature():base() { Hits = 1; }

        public Creature(string imageCharacter, ConsoleColor color, Point position)
        	:base(imageCharacter,color,position)
        {     
            
            Hits = 1;
        } 
        
        public Creature(Point position,TileData tileData):base(position,tileData)
        {
        	Hits = 1;
        }
        #endregion

        #region properties
        
        public bool Alive
        {
            get { return (Hits >= 0); }
        }
        #endregion

        #region methods
        /// <summary>
        /// Method for creature dead event.
        /// </summary>
        /// <param name="sender"></param>
        public delegate void CreatureDeadHandler(Object sender);   

        public int Hits
        {
            get
            {
                return creatureData.Hits;
            }
            set
            {
                creatureData.Hits = value;
                if (creatureData.Hits <= 0)
                {
                    if (CreatureDeadEvent != null)
                    {
                        CreatureDeadEvent(this);
                    }
                }
            }
        }

        public void CalculateMoves(Point target)
        { 
        	Moves.Clear();
        	Moves.Add(Direction.N,Dungeon.GetDistance(Dungeon.AddPoints(Position, AM.DirectionVectors["N"]),target));
        	Moves.Add(Direction.NE,Dungeon.GetDistance(Dungeon.AddPoints(Position, AM.DirectionVectors["NE"]), target));
        	Moves.Add(Direction.E,Dungeon.GetDistance(Dungeon.AddPoints(Position, AM.DirectionVectors["E"]), target));
        	Moves.Add(Direction.SE,Dungeon.GetDistance(Dungeon.AddPoints(Position, AM.DirectionVectors["SE"]), target));
        	Moves.Add(Direction.S,Dungeon.GetDistance(Dungeon.AddPoints(Position, AM.DirectionVectors["S"]), target));
        	Moves.Add(Direction.SW,Dungeon.GetDistance(Dungeon.AddPoints(Position, AM.DirectionVectors["SW"]), target));
        	Moves.Add(Direction.W,Dungeon.GetDistance(Dungeon.AddPoints(Position, AM.DirectionVectors["W"]), target));
        	Moves.Add(Direction.NW,Dungeon.GetDistance(Dungeon.AddPoints(Position, AM.DirectionVectors["NW"]), target));
        }
        
        public void ShootFireball(Direction direction)
        {
        	DungeonManager.Instance.PropManager.Fireball = new Fireball(Position, direction);
            DungeonManager.Instance.PropManager.Fireball.Reset();
            hasFired = true;
        }

        #endregion

    }
}
